package org.example.covid.partion;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
import org.example.covid.entity.CovidPartitionEntity;

import java.util.HashMap;

public class StatePartitioner extends Partitioner<CovidPartitionEntity, Text> {

    public static HashMap<String,Integer> stateMap = new HashMap<String, Integer>();

    static {
        stateMap.put("California",0);
        stateMap.put("Texas",1);
        stateMap.put("Florida",2);
        stateMap.put("Puerto Rico",3);
        stateMap.put("New York",4);
    }
    /**
     *  todo 自定义分区器中分区规则的实现方法，只要getPartition返回的int一样，数据就会被分到同一个分区
     *  同一个分区指的是 数据到同一个reducetask处理
     * @param key   state:州
     * @param value 一行文本数据：2022-05-11,Autauga,Alabama,1001,15853,216
     * @param numPartitions
     * @return
     */
    @Override
    public int getPartition(CovidPartitionEntity key, Text value, int numPartitions) {
        Integer code = stateMap.get(key.getState());
        if (code != null) {
            return code;
        }

        return 5;
    }
}
